<?php

//import enums
require_once ("../enums/enums.php");

//import service
require_once ("../services/AuthentificationService.php");
require_once ("../services/ClientService.php");

//import DO
require_once ("../persistance/data/ResponsableDO.php");
require_once ("../persistance/data/GestionnaireDO.php");
require_once ("../persistance/data/CommandeFactureDO.php");
require_once ("../persistance/data/LieuDO.php");
require_once ("../persistance/data/DetailDO.php");
require_once ("../persistance/data/TypeArticleDO.php");
require_once ("../persistance/data/ArticleDO.php");

//import DAO
require_once ("../persistance/dao/ResponsableDAO.php");
require_once ("../persistance/dao/GestionnaireDAO.php");
require_once ("../persistance/dao/CommandeFactureDAO.php");
require_once ("../persistance/dao/LieuDAO.php");
require_once ("../persistance/dao/DetailDAO.php");
require_once ("../persistance/dao/TypeArticleDAO.php");
require_once ("../persistance/dao/ArticleDAO.php");

//import Bean data
require_once ("../bean/data/Commande.php");
require_once ("../bean/data/Utilisateur.php");
require_once ("../bean/data/Lieu.php");
require_once ("../bean/data/Facture.php");
require_once ("../bean/data/Article.php");

/**
 *	Service disponnible a tout admin
 *
 * @author Quentin Laujac, Charles Hurtevent
 *
 */
class AdminService{
	private $commandeDAO;
	private $responsableDAO;
	private $lieuDAO;
	private $detailDAO;
	private $typeArticleDAO;
	private $articleDAO;
	private $authentificationService;
	private $clientService;


	public function __construct() {
		$this->commandeDAO = new CommandeFactureDAO();
		$this->responsableDAO = new ResponsableDAO();
		$this->lieuDAO = new LieuDAO();
		$this->detailDAO = new DetailDAO();
		$this->typeArticleDAO = new TypeArticleDAO();
		$this->articleDAO = new ArticleDAO();
		$this->authentificationService = new AuthentificationService();
		$this->clientService = new ClientService();
	}

	/**
	 * Retourne les commandes validees par le client et non facturees
	 */
	public function getListCommandeNonFacturees(){
		$listCommandeDO = $this->commandeDAO->getListCommandeFactureDONonFacture();
		if ($listCommandeDO == null) {
			return null;
		}

		$listcommande = new ArrayObject ();

		foreach ( $listCommandeDO as $commandeFactureDO ) {
			$numCommande = $commandeFactureDO->getNumeroCommande();
			$lieuDO = $this->getLieuDO($commandeFactureDO);
			$lieu = new Lieu();
			$lieu->setAdresse($lieuDO->getAdresse());
			$lieu->setNumeroLieu($lieuDO->getNumeroLieu());
			$listArticle = $this->clientService->getListArticleBynumCommande($numCommande);
			$client = $this->authentificationService->getClientByNumResponsable($commandeFactureDO->getNumeroResponsable());
			$commande = $this->convertCommandeFactureDOtoCommande ( $commandeFactureDO, $lieu, $client, $listArticle);
			$listcommande->append ($commande);
		}
		return $listcommande;
	}

	/**
	 *
	 * Met a jour la commande
	 * @param Commande $commande
	 */
	public function facturerCommande(Commande $commande) {
		$commandeFactureDO = $this->convertCommandeToCommandeFactureDO($commande);
		$this->commandeDAO->updateCommandeFactureDO($commandeFactureDO);
	}

	/**
	 *
	 * Met a jour la commande
	 * @param Commande $commande
	 */
	public function faireColis(Commande $commande) {
		$commandeFactureDO = $this->convertCommandeToCommandeFactureDO($commande);
		$this->commandeDAO->updateCommandeFactureDO($commandeFactureDO);
	}

	/**
	 * Retourne la liste des commande validee par le client et facturees
	 */
	public function getListCommandeFacturees(){
		$listCommandeDO = $this->commandeDAO->getListCommandeFactureDOFacture();
		if ($listCommandeDO == null) {
			return null;
		}

		$listcommande = new ArrayObject ();

		foreach ( $listCommandeDO as $commandeFactureDO ) {
			$numCommande = $commandeFactureDO->getNumeroCommande();
			$lieuDO = $this->getLieuDO($commandeFactureDO);
			$lieu = new Lieu();
			$lieu->setAdresse($lieuDO->getAdresse());
			$lieu->setNumeroLieu($lieuDO->getNumeroLieu());
			$listArticle = $this->clientService->getListArticleBynumCommande($numCommande);
			$client = $this->authentificationService->getClientByNumResponsable($commandeFactureDO->getNumeroResponsable());
			$commande = $this->convertCommandeFactureDOtoCommande ( $commandeFactureDO, $lieu, $client, $listArticle);
			$listcommande->append ($commande);
		}
		return $listcommande;
	}



	//------------------------GETTER private-------------------------------------------------------------
	private function getDernierNumeroCommande(){
		return $this->commandeDAO->getDernierNumCommande();
	}

	private function getArticleDOByDetailDO(DetailDO $detailDO){
		return $this->articleDAO->getArticleDO($detailDO->getNumeroArticle());
	}

	private function getListLieu(ResponsableDO $responsableDO){
		$listLieu = new ArrayObject();
		$listLieuDO = $this->lieuDAO->getListLieuDOByNumStructure($responsableDO->getNumeroStructure());

		foreach ( $listLieuDO as $lieuDO ) {
			$listLieu->append ( $this->convertLieuDOtoLieu ( $lieuDO ) );

		}
		return $listLieu;

	}

	private function getLieuDO(CommandeFactureDO $commandeFactureDO){
		$lieuDO = $this->lieuDAO->getLieuDOByNumLieu($commandeFactureDO->getNumeroLieu());

		return $this->convertLieuDOtoLieu($lieuDO);
	}






	//-----------------------CONVERTION-----------------------------------------------------------------------

	private function convertCommandeToCommandeFactureDO(Commande $commande){
		$commandeFactureDO = new CommandeFactureDO();
		$commandeFactureDO->setDateCommande($commande->getDateCommande());
		$commandeFactureDO->setDateFacture($commande->getFacture()->getDateFacture());
		$commandeFactureDO->setEstColisFait($commande->isColisFait());
		$commandeFactureDO->setEstPaye(true);
		$commandeFactureDO->setEstTerminee($commande->isValide());
		$commandeFactureDO->setMontantFacture($commande->getFacture()->getMontantFacture());
		$commandeFactureDO->setNumeroCommande($commande->getNumeroCommande());
		$commandeFactureDO->setNumeroLieu($commande->getLieuLivraison()->getNumeroLieu());
		$commandeFactureDO->setNumeroResponsable($commande->getClient()->getNumeroResponsable());

		return $commandeFactureDO;
	}

	/**
	 * retire de la listDetailDO tout les detailsDO ayant une quantite egale a 0
	 * @param ArrayObject $listDetailDO
	 */
	private function purgeListDetailDO(ArrayObject $listDetailDO){
		$listDetailDOPurge = new ArrayObject();
		foreach ($listDetailDO as $detailDO){
			if($detailDO->getQuantiteCommande() > 0){
				$listDetailDOPurge->append($detailDO);
			}
		}

		return $listDetailDOPurge;
	}

	private function convertListArticleToListDetailDO(ArrayObject $listArticle, $numCommande){
		$listDetailDO = new ArrayObject();
		foreach($listArticle as $article){
			$detailDO = new DetailDO();
			$detailDO->setNumeroArticle($article->getNumArticle());
			$detailDO->setNumeroCommande($numCommande);
			$detailDO->setQuantiteCommandee($article->getQuantiteCommandee());

			$listDetailDO->append($detailDO);
		}

		return $listDetailDO;
	}

	private function convertDetailDOtoArticle(DetailDO $detailDO, ArticleDO $articleDO, TypeArticleDO $typeArticleDO){
		$article = new Article();
		$article->setLibelleTypeArticle($typeArticleDO->getLibelle());
		$article->setLibelle($articleDO->getLibelle());
		$article->setNumArticle($articleDO->getNumArticle());
		$article->setPrixUnitaire($articleDO->getPrixUnitaire());
		$article->setQuantiteCommandee($detailDO->getQuantiteCommande());
		$article->setQuantiteDisponible($articleDO->getQuantiteDisponible());

		return $article;
	}

	private function convertLieuDOtoLieu(LieuDO $lieuDO){
		$lieu = new Lieu();
		$lieu->setAdresse($lieuDO->getAdresse());
		$lieu->setNumeroLieu($lieuDO->getNumeroLieu());
		return $lieu;
	}

	private function convertCommandeFactureDOtoCommande(CommandeFactureDO $commandeFactureDO, Lieu $lieu, Utilisateur $client, $listArticle) {
		$facture = new Facture();
		$facture->setDateFacture($commandeFactureDO->getDateFacture());
		$facture->setMontantFacture($commandeFactureDO->getMontantFacture());

		$commande = new Commande ();
		$commande->setClient($client);
		$commande->setDateCommande($commandeFactureDO->getDateCommande());
		$commande->setEstValide($commandeFactureDO->estTerminee());
		$commande->setColisFait($commandeFactureDO->estColisFait());


		$commande->setFacture($facture);
		$commande->setLieuLivraison($lieu);
		$commande->setListArticle($listArticle);
		$commande->setNumeroCommande($commandeFactureDO->getNumeroCommande());

		return $commande;

	}

	private function convertResponsableDOtoUtilisateur(ResponsableDO $responsableDO){
		$utilisateur = new Utilisateur();
		$utilisateur->setNom($responsableDO->getNom());
		$utilisateur->setPrenom($responsableDO->getPrenom());
		$utilisateur->setListLieu($this->getListLieu($responsableDO));
		$utilisateur->setEstResponsable(true);
		$utilisateur->setNumeroResponsable($responsableDO->getNumeroResponsable());

		return $utilisateur;
	}
}
